package cloud.authentication.server.authentication.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
/**
 * 参考 http://www.leftso.com/blog/139.html
 * @author Administrator
 *
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
	
	 /*@Resource
	    private DruidDataSource dataSource;
*/
	@Value("${spring.security.oauth2.jwt.signingKey}")
	private String signingKey;
	
	

	    @Override
	    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
	        resources.tokenStore(tokenStore())
	        .resourceId("WEBS");
	    }

	    /*@Bean
	    @Primary
	    public DefaultTokenServices tokenServices() {
	        final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
	        defaultTokenServices.setTokenStore(tokenStore());
	        return defaultTokenServices;
	    }*/
	    @Bean
	    public JwtAccessTokenConverter accessTokenConverter(){
	    	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	    	converter.setSigningKey(signingKey);
	    	return converter;
	    }

	    @Bean
	    public TokenStore tokenStore() {
	       // return new JdbcTokenStore(dataSource);
	    	return new JwtTokenStore(accessTokenConverter());
	    }
}
